﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using MSDAL;
using PagedList;

namespace MSWeb.Controllers
{
    public class InstrumentiesController : Controller
    {
        private readonly MSEntities _db = new MSEntities();

        [Authorize]
        public ActionResult Index(int? page)
        {
            var pageSize = 10;
            var pageNumber = (page ?? 1);
            return View(_db.Instrumenty
                .OrderBy(i => i.IDInstrumentu)
                .ToPagedList(pageNumber, pageSize));
        }


        [Authorize]
        public ActionResult Detale(int? id)
        {
            if (!id.HasValue)
            {
                TempData["Error"] = "Nieodnaleziono podanego rekordu";
                return RedirectToAction("Index");
            }
            if (_db.Instrumenty.Any(i => i.IDInstrumentu == id))
            {
                var instrumenty = _db.Instrumenty.First(i => i.IDInstrumentu == id);
                return PartialView("_DetaleInstrumenty", instrumenty);
            }
            TempData["Error"] = "Nieodnaleziono podanego rekordu";
            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult Edycja(int? id)
        {
            if (!id.HasValue)
            {
                TempData["Error"] = "Nieodnaleziono podanego rekordu";
                return RedirectToAction("Index");
            }
            else
            {
                if (_db.Instrumenty.Any(i => i.IDInstrumentu == id))
                {
                    Instrumenty instrumenty = _db.Instrumenty.First(i => i.IDInstrumentu == id);
                    return PartialView("_EdycjaInstrumentu", instrumenty);
                }
                else
                {
                    TempData["Error"] = "Nieodnaleziono podanego rekordu";
                    return RedirectToAction("Index");
                }
            }
        }

        [Authorize]
        public ActionResult Tworzenie()
        {
            return PartialView("_TworzenieInstrumenty");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Instrumenty instrument)
        {
            _db.Instrumenty.Add(instrument);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Instrumenty instrument)
        {
            try
            {
                _db.Entry(instrument).State = EntityState.Modified;
                _db.SaveChanges();
            }
            catch (Exception)
            {
                // ignored
            }
            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                TempData["Error"] = "Nieodnaleziono podanego rekordu";
                return RedirectToAction("Index");
            }
            var instrumenty = _db.Instrumenty.Find(id);
            if (instrumenty == null)
            {
                return HttpNotFound();
            }
            return View(instrumenty);
        }

        // GET: Instrumenties/Delete/5
        [Authorize]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Instrumenty instrumenty = _db.Instrumenty.Find(id);
            if (instrumenty == null)
            {
                return HttpNotFound();
            }
            return View(instrumenty);
        }

        // POST: Instrumenties/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            Instrumenty instrumenty = _db.Instrumenty.Find(id);
            _db.Instrumenty.Remove(instrumenty);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
